/*
* Copyright (c) 2005, 2014 vacoor
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package lucene.entity;

// import javax.persistence.*;

import org.vacoor.nothing.common.codec.Hex;

import java.nio.charset.Charset;

/**
 * Created on 2014-12-02 11:59:26
 *
 * @author crud generated
 */
/*
@Entity
@Table(name = "TB_CAR_MODEL")
@PropOrder({
"id",
"name",
"year",
"power",
"seriesId",
"deleted",
"createdDate",
"createdBy",
"lastModifiedDate",
"lastModifiedBy"
})
*/
public class CarModel extends NamedEntity {
    private String year;        // 年款 这里其实最好单独拉出来, 但是考虑, 年款，排量量不会大，因此省事
    private String power;
    private CarSeries carSeries;

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPower() {
        return power;
    }

    public void setPower(String power) {
        this.power = power;
    }

    public CarSeries getCarSeries() {
        return carSeries;
    }

    public void setCarSeries(CarSeries carSeries) {
        this.carSeries = carSeries;
    }

    /**
     * 根据车系 id 和 power 生成 power id
     * <p/>
     * 因为现在 power 是没有数据库表的, 所以这里主要是为了生成一个虚拟 id
     *
     * @param seriesId
     * @param power
     * @return
     */
    public static String encodePower(String seriesId, String power) {
        if (null == seriesId || null == power) {
            return null;
        }
        return "CP" + seriesId + "_S_" + Hex.encode(power.getBytes(Charset.forName("UTF-8")));
    }

    /**
     * 解码车系Id
     *
     * @param encodedPower
     * @return
     */
    public static String[] decodePower(String encodedPower) {
        if (null == encodedPower || !encodedPower.startsWith("CP")) {
            return null;
        }
        String[] pairts = encodedPower.substring(2).split("_S_");
        if (2 != pairts.length) {
            return null;
        }
        pairts[1] = new String(Hex.decode(pairts[1]), Charset.forName("UTF-8"));
        return pairts;
    }
}
